$(function () {
    var address_id = parseInt(GetQueryString('address_id'));
    var payorder_id = parseInt(GetQueryString('payorder_id'));
    var html_data = {
        baseConfig: baseConfig,
        sessionid: '',
        sessionid_q: '',
        address_info: {},
        payorder_id: payorder_id,
        province_list: [],
        city_list: [],
        county_list: [],
        address_list: []

    };
    var app = new Vue({
        el: '#address_modefy',
        data: html_data,
        methods: {
            query_county: function () {
                api.lorder.get_counties_list({city_id: html_data.address_info.receiver_city_id}).then(function (citydata) {
                    html_data.county_list = citydata.result.list;
                });
            },
            query_citys: function () {
                api.lorder.get_cities_list({province_id: html_data.address_info.receiver_province_id}).then(function (citydata) {
                    html_data.city_list = citydata.result.list;
                });
            },
            modefy_address: function () {
                var parm = {};
                if (html_data.address_info.receiver_name) {
                    parm.receiver_name = html_data.address_info.receiver_name;
                } else {
                    Magiclamp.mcAlert("提示", "收货人不能为空！");
                    return;
                }
                if (html_data.address_info.receiver_phone) {
                    var sMobile = html_data.address_info.receiver_phone
                    if (!(/^1[3|4|5|7|8][0-9]\d{4,8}$/.test(sMobile))) {
                        Magiclamp.mcAlert("提示", "不是完整的11位手机号或者正确的手机号前七位！");
                        return false;
                    }
                    parm.receiver_phone = html_data.address_info.receiver_phone;
                } else {
                    Magiclamp.mcAlert("提示", "手机号码不能为空！");
                    return;
                }

                if (parseInt(html_data.address_info.receiver_province_id)>0) {
                    parm.receiver_province_id = html_data.address_info.receiver_province_id;
                } else {
                    Magiclamp.mcAlert("提示", "省份不能为空！");
                    return;
                }

                if (parseInt(html_data.address_info.receiver_city_id)>0) {
                    parm.receiver_city_id = html_data.address_info.receiver_city_id;
                } else {
                    Magiclamp.mcAlert("提示", "城市不能为空！");
                    return;
                }
                if (parseInt(html_data.address_info.receiver_county_id)>0) {
                    parm.receiver_county_id = html_data.address_info.receiver_county_id;
                } else {
                    Magiclamp.mcAlert("提示", "地区不能为空！");
                    return;
                }
                if (html_data.address_info.receiver_address_detail) {
                    parm.receiver_address_detail = html_data.address_info.receiver_address_detail;
                } else {
                    Magiclamp.mcAlert("提示", "详细地址不能为空！");
                    return;
                }
                parm.is_default = html_data.address_info.is_default;
                if (html_data.address_info.id) {
                    parm.address_id = html_data.address_info.id;
                    if (payorder_id) {
                        parm.payorder_id = payorder_id;
                    }
                    api.lorder.update_address(parm).then(function (data) {
                        history.go(-1);
                    });
                } else {
                    api.lorder.create_address(parm).then(function (data) {
                        history.go(-1);
                    })
                }

            },
            page_back: function () {
                baseConfig.page_back();
            }
        }
    });

    html_data.sessionid = GetQueryString('sessionid');
    if (html_data.sessionid) {
        html_data.sessionid_q = "sessionid=" + html_data.sessionid;
        client_storage('sessionid', html_data.sessionid);
    }

    api.lorder.get_provinces_list().then(function (data) {
        html_data.province_list = data.result.list;
    });

    if (address_id) {
        api.lorder.get_address({address_id: address_id}).then(function (data) {
            html_data.address_info = data.result;

            api.lorder.get_cities_list({province_id: html_data.address_info.receiver_province_id}).then(function (citydata) {
                html_data.city_list = citydata.result.list;
            });
            api.lorder.get_counties_list({city_id: html_data.address_info.receiver_city_id}).then(function (citydata) {
                html_data.county_list = citydata.result.list;
            });
        });
    } else {
        html_data.address_info = {receiver_province_id: 0, receiver_city_id: 0, receiver_county_id: 0};
    }


});
